/**
 * Created by huangyu on 16-5-23.
 */

var http = require('http');
var queryString = require("querystring");


http.createServer(function (request, response) {
    //request.parse();
    var postData = "";
    request.setEncoding("utf8");

    request.addListener("data", function (postDataChunk) {
        postData += postDataChunk;
        //console.log("Received POST data chunk '"+
        //    postDataChunk +"'.")
    });
    request.addListener("end", function () {
        console.log(postData);
        console.log('\n');
        var base64Data = queryString.parse(postData);

        //bas64 decode
        var debase64_str = Buffer(base64Data["data"], 'base64').toString();

        var raw_js_str = debase64_str.replace('<script>', '').replace('</script>', '').replace('eval', 'var raw_js_str=');
        //var raw_js_str = debase64_str.replace('<script>', '').replace('</script>', '');
        eval(raw_js_str);
        //raw_js_str = eval(raw_js_str);
        //console.log(eval(raw_js_str));

        console.log(raw_js_str);

        raw_js_str = raw_js_str.replace('while(window._phantom||window.__phantomas){};', '');
        var result_1 = raw_js_str.replace(new RegExp('setTimeout.*}'), 'return dc }; l();');

        var result_2 = eval(result_1);
        console.log(result_2);

        var result_2_base64 = new Buffer(result_2).toString('base64');

        response.write(result_2_base64);
        response.end();

        //raw_js_str = raw_js_str.replace('while(window._phantom||window.__phantomas){};', '').replace(new RegExp('setTimeout.*}'),'return cd };');

    });

    //response.write(result_2_base64);
    //response.end();
}).listen(44449, '0.0.0.0');

console.log('server start');

